using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;
using System.Collections;
using Microsoft.SharePoint.Workflow;

namespace PropertyManagementFeatures
{
    class PropertyMangementWorkflowFeature : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList taskList;
                    SPList historyList;
                    SPList propertyList;

                    //associate with task list 
                    try
                    {
                        taskList = web.Lists["Property Management Tasks"];
                    }
                    catch (Exception ex)
                    {
                        Guid taskListId = web.Lists.Add("Property Management Tasks", "Workflow Task", SPListTemplateType.Tasks);
                        taskList = web.Lists[taskListId];
                        
                        SPContentType propertyEvaluationContentType = web.ContentTypes[new SPContentTypeId("0x0108010084565D92BEFE4a75A28C2F658B7B1234")];
                        SPContentType propertyOnlineContentType = web.ContentTypes[new SPContentTypeId("0x0108010084565D92BEFE4a75A28C2F658B7B1222")];
                        SPContentType propertySaleContentType = web.ContentTypes[new SPContentTypeId("0x0108010084565D92BEFE4a75A28C2F658B7B3333")];
                        taskList.ContentTypes.Add(propertyEvaluationContentType);
                        taskList.ContentTypes.Add(propertyOnlineContentType);
                        taskList.ContentTypes.Add(propertySaleContentType);
                        taskList.Update();
                    }

                    //associate with history list
                    try
                    {
                        historyList = web.Lists["Workflow History"];
                    }
                    catch (Exception ex)
                    {

                        Guid historyListId = web.Lists.Add("Workflow History", "Workflow History", SPListTemplateType.WorkflowHistory);
                        historyList = web.Lists[historyListId];
                    }

                    propertyList = web.Lists["Properties"];

                    //associate workflow to property list
                    SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates[new Guid("81b291a0-5a0f-461f-92b3-53287c7e02af")];
                    SPWorkflowAssociation workflowAssociation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, "Property Management Workflow", taskList, historyList);
                    workflowAssociation.AllowManual = false;
                    workflowAssociation.AutoStartCreate = true;
                    workflowAssociation.AutoStartChange = false;

                    workflowAssociation = propertyList.AddWorkflowAssociation(workflowAssociation);
                    workflowAssociation.Enabled = true;

                }
            }
            
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

    }
}
